import React, { JSX } from 'react';
import { NulDialog, NulCommonOperations, NulFormItem } from '../../components/NulDialog';
import { FormControl, MenuItem, Select, TextField, Typography } from '@mui/material';
import { useAppDispatch } from '../../actions/hook';
import '@fontsource/open-sans';
import { UserEntry, modifyUser } from '../../actions/UserManagement';

interface ModifyUserProps {
  open: boolean;
  onClose: () => void;
  customerEntry: UserEntry;
}

function ModifyUser(props: ModifyUserProps): JSX.Element {
  const { open, onClose, customerEntry } = props;

  const [name, setName] = React.useState<string>(customerEntry.name);
  const [userName, setUserName] = React.useState<string>(customerEntry.userName);
  const [gender, setGender] = React.useState<string>(customerEntry.gender);
  const [authority, setAuthority] = React.useState<string>(customerEntry.authority);

  React.useEffect(() => {
    setName(customerEntry.name);
    setUserName(customerEntry.userName);
    setGender(customerEntry.gender);
    setAuthority(customerEntry.authority);
  }, [customerEntry]);

  const dispatch = useAppDispatch();

  const onModifyClicked = () => {
    dispatch(modifyUser({
      id: customerEntry.id,
      name, userName, gender, authority
    }));
    onClose();
  };

  return (
    <NulDialog
      open={open}
      onClose={onClose}
      title='Edit User'
      operations={
        <NulCommonOperations
          onPositiveClicked={onModifyClicked}
          onNegativeClicked={onClose}
          positiveText='Modify'
        />
      }
    >
      <NulFormItem hint='UID' necessary={false} component={
        <Typography>{customerEntry.id}</Typography>
      } />
      <NulFormItem hint='User Name' component={
        <TextField placeholder='User Name' fullWidth={true} size='small' value={userName} onChange={(event) => { setUserName(event.target.value); }} />
      } />
      <NulFormItem hint='Name' component={
        <TextField placeholder='Name' fullWidth={true} size='small' value={name} onChange={(event) => { setName(event.target.value); }} />
      } />
      <NulFormItem hint='Gender' component={
        <FormControl fullWidth size='small'>
          <Select
            labelId="type-select-label"
            id="type-select"
            value={gender}
            placeholder="Type"
            onChange={(event) => {
              if (event.target.value === 'male' || event.target.value === 'female')
                setGender(event.target.value);
            }}
          >
            <MenuItem value='male'>Male</MenuItem>
            <MenuItem value='female'>Female</MenuItem>
          </Select>
        </FormControl>
      } />
      <NulFormItem hint='Authority' component={
        <FormControl fullWidth size='small'>
          <Select
            labelId="type-select-label"
            id="type-select"
            value={authority}
            placeholder="Type"
            onChange={(event) => {
              if (event.target.value === 'user' || event.target.value === 'admin')
                setAuthority(event.target.value);
            }}
          >
            <MenuItem value='user'>User</MenuItem>
            <MenuItem value='admin'>Administrator</MenuItem>
          </Select>
        </FormControl>
      } />
    </NulDialog>
  );
}

export { ModifyUser };